//
//  SEHospitalizeSectionHeader.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/2.
//

import UIKit

enum SEHospitalizeSection: Int {
    case location
    case service
    case headlines
    case unknown = -1
    
    var title: String? {
        switch self {
        case .location:  return "附近检查点"
        case .service:   return "专科服务"
        case .headlines: return "健康头条"
        default:         return nil
        }
    }
    
    var hideLocationBtn: Bool {
        switch self {
        case .location: return false
        default:        return true
        }
    }
    
    var moreButtonHidden: Bool {
        switch self {
        case .headlines: return true
        default:         return false
        }
    }
}

class SEHospitalizeSectionHeader: UIView {
    
    typealias SelectedClosure = (SEHospitalizeSection) -> Void
    typealias LocatedClosure = (SEHospitalizeSection) -> Void
    
    var selectedClosure: SelectedClosure?
    var locatedClosure: LocatedClosure?


    @IBOutlet private weak var titleLabel: UILabel!
    @IBOutlet private weak var locationBtn: UIButton! {
        didSet {
            locationBtn.layer.cornerRadius = locationBtn.height / 2
            locationBtn.layer.borderWidth = 1
            locationBtn.layer.borderColor = UIColor(hex: "#C9D8EC").withAlphaComponent(0.42).cgColor
            locationBtn.backgroundColor = UIColor(hex: "#ECF6FE")
            locationBtn.layoutButton(with: .imageLeft, space: 2)
        }
    }
    @IBOutlet private weak var moreButton: UIButton! {
        didSet {
            moreButton.layoutButton(with: .imageRight, space: 1)
        }
    }
    
    private var headerSection: SEHospitalizeSection = .unknown
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = UIColor.white
    }
    
    func updateUI(section: SEHospitalizeSection, btnTitle: String = "", selected: SelectedClosure?, located: LocatedClosure?) {
        headerSection = section
        selectedClosure = selected
        locatedClosure = located
        titleLabel.text = section.title
        locationBtn.isHidden = section.hideLocationBtn
        moreButton.isHidden = section.moreButtonHidden
        
        if section == .location {
            setLayerCorners(rect: bounds, radius: 20, corners: [.topLeft, .topRight])
            locationBtn.setTitle(btnTitle, for: .normal)
        }
    }
    
    @IBAction private func openLocationAction() {
        guard headerSection == .location else { return }
        locatedClosure?(headerSection)
    }
    
    @IBAction private func viewAllAction() {
        selectedClosure?(headerSection)
    }
}
